package api

import (
	"examination-management/api/cmd/config"
	"examination-management/api/handler/request"
	"examination-management/api/pkg"
	pb "examination-management/api/proto"
	"github.com/dgrijalva/jwt-go"
	"github.com/gin-gonic/gin"
	jwt2 "github.com/golangblogs/gojwt"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"log"
	"time"
)

func CreateUser(c *gin.Context) {
	var req request.CreateUserReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	client := pb.NewExaminationClient(conn)

	user, _ := client.CreateUser(c, &pb.CreateUserReq{
		Mobile:   req.Mobile,
		Password: pkg.Md5(req.Password),
	})
	c.JSON(200, user)
}

func LoginUser(c *gin.Context) {
	var req request.LoginUserReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	client := pb.NewExaminationClient(conn)

	user, _ := client.LoginUser(c, &pb.LoginUserReq{
		Mobile:   req.Mobile,
		Password: pkg.Md5(req.Password),
	})
	if user.Code != 200 {
		c.JSON(200, user)
		return
	}
	token, _ := jwt2.NewJWT(config.JWT_SELECT_KEY).CreateToken(jwt2.CustomClaims{
		ID: uint(user.Id),
		StandardClaims: jwt.StandardClaims{
			ExpiresAt: time.Now().Unix() + 86400,
		},
	})
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": token,
	})
}
func UpdateUser(c *gin.Context) {
	var req request.UpdateUserReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	client := pb.NewExaminationClient(conn)

	user, _ := client.UpdateUser(c, &pb.UpdateUserReq{
		Id:    uint64(c.GetUint("userId")),
		Name:  req.Name,
		UType: req.UType,
	})
	c.JSON(200, user)
}

func QueryUserInfo(c *gin.Context) {
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	client := pb.NewExaminationClient(conn)

	user, _ := client.QueryUserInfo(c, &pb.QueryUserInfoReq{
		Id: uint64(c.GetUint("userId")),
	})
	c.JSON(200, user)
}

func QueryUserInfoTime(c *gin.Context) {
	var req request.QueryUserInfoTimeReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	conn, err := grpc.NewClient("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	client := pb.NewExaminationClient(conn)

	user, _ := client.QueryUserInfoTime(c, &pb.QueryUserInfoTimeReq{
		Id:     uint64(c.GetUint("userId")),
		InfoId: req.Id,
	})
	c.JSON(200, user)
}
